package pl.edu.pw.elka.tinyrepo.client.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.concurrent.BlockingQueue;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;

import pl.edu.pw.elka.tinyrepo.client.common.DirNode;
import pl.edu.pw.elka.tinyrepo.client.common.FileNode;
import pl.edu.pw.elka.tinyrepo.client.common.MockRequest;
import pl.edu.pw.elka.tinyrepo.client.common.RepoTreeModel;
import pl.edu.pw.elka.tinyrepo.common.CatalogContent;
import pl.edu.pw.elka.tinyrepo.common.CatalogContentSerializer;
import sun.management.jmxremote.ConnectorBootstrap.DefaultValues;

/**
 * 
 * @author Anna Stępień
 * @version 13-12-2011
 *
 */
class RepoTreePanel extends JPanel
{

	private static final long serialVersionUID = 1L;
	/** Kolejka blokująca Requestów */
	private final BlockingQueue<MockRequest> requestQueue;
	
	private final JTree tree;
	
	public RepoTreePanel(final BlockingQueue<MockRequest> requestQueue)
	{
		this.requestQueue = requestQueue;
		setLayout(new BorderLayout());
		
		tree = new JTree();
		add(tree, BorderLayout.LINE_START);
		tree.setVisible(false);
		tree.setCellRenderer(new CustomNodeRenderer());
		setBackground(Color.WHITE);
		setPreferredSize(new Dimension(340, 480));
	}
	
	public void setModel(final TreeModel model)
	{
		tree.setModel(model);
		tree.setVisible(true);

	}
	
	private class CustomNodeRenderer extends DefaultTreeCellRenderer
	{

		private static final long serialVersionUID = 1L;
		
		public CustomNodeRenderer()
		{
			super();
			setOpenIcon(new ImageIcon("images/folder.png"));
			setClosedIcon(new ImageIcon("images/folder.png"));
			setLeafIcon(new ImageIcon("images/folder.png"));
		}
		
        public Component getTreeCellRendererComponent(JTree tree, Object value,
                boolean selection, boolean expanded,
                boolean leaf, int row, boolean hasFocus)
		{
			super.getTreeCellRendererComponent(tree, value, selection, expanded, 
			         leaf, row, hasFocus);
			
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
			
			setCustomIcon(node, tree);
			
			return this;
		}
        
        public void setCustomIcon(DefaultMutableTreeNode node, JTree tree)
        {
        	if (node instanceof DirNode)
        	{
        		setIcon(((DirNode) node).getIcon());
        	}
        	if (node instanceof FileNode)
        	{
        		setIcon(((FileNode) node).getIcon());
        	}
        }
		
	}
}
